#include "Splines.h"
#include <vector>
#include <iostream>

class f
{
public:
    double operator()(double x)
	{
	    double y;
	    y = 1/(1+25*x*x);
	    return y;
	};
};

class getX
{
public:
    vector<double> operator()(double N)
	{
	    vector<double> X0;
	    X0.resize(N);
	    for (int i = 0; i < N; i++)
	    {
		X0[i] = -1+i*2/(N-1);
	    }
	    return X0;
	};
};

int main()
{
    vector<double> X,Y,N;
    N = {6,11,21,41,81};
    getX getX;
    f f;
    vector<vector<vector<double>>> P_n, P_s, P_c;
    P_n.resize(N.size());
    P_s.resize(N.size());
    P_c.resize(N.size());
    for (int i = 0; i<N.size();i++)
    {
	X = getX(N[i]);
	Y.resize(N[i]);
	for (int j = 0; j < N[i]; j++)
	{
	    Y[j] = f(X[j]);
	}
	ppFrom p(X,Y);
	double a,b;
	P_n[i] = p.solve_n();//自然样条插值
	P_c[i] = p.solve_c(a,b);//完全样条插值，a=f'(-1),b=f'(1)
	P_s[i] = p.solve_s(a,b);//指定二阶导数样条插值，a=f''(-1),b=f''(1)
	cout << endl;
	for (int j = 0; j < N[i] - 1; j++)
	{
	    cout << P_n[i][j][0] << " " << P_n[i][j][1] << " " << P_n[i][j][2] << " " << P_n[i][j][3] << endl;
	}
    }
    ///P中储存样条分段函数的系数，以自然样条插值为例输出
    //cout << "当N = " << N[0] << "时," << endl;
    //for (int j = 0; j < N[0]-1; j++)
    //{
//	cout << "p" << j+1 << "=" << P_n[0][j][0] << "+" << P_n[0][j][1] << "*(x-X["<< j << "]) + " << P_n[0][j][2] << "*(x-X[" << j << "])^2 + "<< P_n[0][j][3] << "*(x-X[" << j << "])^3" << endl;
    //  }

    //for (int i = 0; i < N.size(); i++)
    //{
//	cout << endl;
//	for (int j = 0; j < N[i] - 1; j++)
//	{
//	    cout << P_n[0][j][0] << " " << P_n[0][j][0] << " " << P_n[0][j][0] << " " << P_n[0][j][0] << endl;
//	}
//    }
}
